<?php

namespace App\Http\Controllers;


use Log;
use DB;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Validator;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    const EMPTY_CODE = 100, ERROR_CODE = 200, DB_ERROR_CODE = 300, VALIDATE_ERROR = 400;

    /**
     * Controller constructor.
     */
    public function __construct(Request $request)
    {
        $ip = $request->ip();
        $url = $request->path();
        $input = $request->all();
        Log::info($ip . " " . $url, $input);
        DB::connection()->enableQueryLog();
    }

    /**
     * wirte sql logo to log file
     * @return
     */
    protected function sqlLog()
    {
        if (config('app.debug')) {
            Log::debug(__METHOD__, DB::getQueryLog());
        }
    }

    /**
     * @param $input
     * @param $rules
     * @param $messages
     * @return array
     */
    public function validate($input, $rules, $messages)
    {
        $validator = Validator::make($input, $rules, $messages);
        if ($validator->fails()) {
            $errors = $validator->errors()->all();
            return ["code" => self::VALIDATE_ERROR, "errors" => $errors];
        }
        return ["code" => 0];
    }

    /**
     * response with json
     * @param  string code
     * @param  string msg
     * @param  array data
     * @return Response
     */
    protected function outputJSON($code, $msg = '', $data = [])
    {
        return new JsonResponse(compact('code', 'msg', 'data'), 200);
    }

    /**
     * success response with json
     * @param  array data
     * @return Response
     */
    protected function success($data = [])
    {
        return $this->outputJSON(0, 'ok', $data);
    }

    /**
     * fail response with json
     * @param  array data
     * @param  string msg
     * @return Response
     */
    protected function fail($code, $msg, $data = [])
    {
        return $this->outputJSON($code, $msg, $data);
    }

    /**
     * JSON 响应
     *
     * @param     $data
     * @param int $code
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function responseJson($data, $code = 200)
    {
        return response()->json($data, $code);
    }

    /**
     * 成功时路由跳转
     *
     * @param $route
     * @param $message
     *
     * @return mixed
     */
    public function successRoutTo($route, $message)
    {
        return redirect()->route($route)->withSuccess($message);
    }

    /**
     * 成功时返回当前页
     *
     * @param $message
     *
     * @return mixed
     */
    public function successBackTo($message)
    {
        return redirect()->back()->withSuccess($message);
    }

    /**
     * 失败时路由跳转
     *
     * @param $route
     * @param $message
     *
     * @return $this
     */
    public function errorRouteTo($route, $message)
    {
        return redirect()->route($route)->withErrors($message);
    }

    /**
     * 失败时返回当前页
     *
     * @param $message
     *
     * @return $this
     */
    public function errorBackTo($message)
    {
        return redirect()->back()->withErrors($message)->withInput();
    }
}
